<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if ($route->GET("insurer"))
{
	$insurer = urldecode($route->GET("insurer"));
	$city = urldecode($route->GET("city"));
	$str = "[";
	if ($city != "")
		$result = $db->getCell("select `I`.`name` from `companies` as `I`,`city` as `C` where INSTR(`I`.`name`, '$insurer') = 1 and `C`.`city_id` = `I`.`city_id` and `C`.`name` like '%$city%' order by `name` limit 1");
	else
		$result = $db->getCell("select `name` from `companies` where INSTR(`name`, '$insurer') = 1 and `head_office` = '0' order by `name` limit 1");
	if ($result != "")
		$result = substr_replace($result,$insurer,0,strlen($insurer));
	$str .= "{\"tip\":\"$result\"}";
	if ($city != "")
		$result = $db->getColumn("select `I`.`name` from `companies` as `I`,`city` as `C` where `I`.`name` like '%$insurer%' and `C`.`city_id` = `I`.`city_id` and `C`.`name` like '%$city%' order by `name`");
	else
		$result = $db->getColumn("select `name` from `companies` where `name` like '$insurer%' and `head_office` = '0' order by `name`");
	if (count($result) > 0)
	{
		$str .= ",{\"name\":\"";
		$str .= implode("\"},{\"name\":\"",$result);
		$str .= "\"}";
	}
	$str .= "]";
	echo $str;
}
else
{
	$city = urldecode($route->GET("city"));
	$str = "[{\"tip\":\"\"}";
	if ($city != "")
		$result = $db->getColumn("select `I`.`name` from `companies` as `I`,`city` as `C` where `C`.`city_id` = `I`.`city_id` and `C`.`name` like '%$city%' order by `name`");
	else
		$result = $db->getColumn("select `name` from `companies` where `head_office` = '0' order by `name`");
	if (count($result) > 0)
	{
		$str .= ",{\"name\":\"";
		$str .= implode("\"},{\"name\":\"",$result);
		$str .= "\"}";
	}
	$str .= "]";
	echo $str;
}
?>